package net.messagenet.mazor.web.mngbeans;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.messagenet.mazor.ejb.facade.MZRFacadeLocal;
import net.messagenet.mazor.exception.MZRApplicativeException;
import net.messagenet.mazor.exception.MZRInternalServerException;
import net.messagenet.mazor.web.DO.DpaFile;

import org.ajax4jsf.model.KeepAlive;
import org.apache.log4j.Logger;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

@KeepAlive
public class DpaUploadManagedBean implements Serializable{

	     
	     /**
	 * 
	 */
	private static final long serialVersionUID = -2724193333176087162L;
	private Logger logger = Logger.getLogger(this.getClass());
	
	
		private ArrayList<DpaFile> files = new ArrayList<DpaFile>();
	     private int uploadsAvailable = 5;
	     private boolean autoUpload = false;
	     private boolean useFlash = false;
		private MZRFacadeLocal facade;
	    
	     public int getSize() {
	         if (getFiles().size()>0){
	             return getFiles().size();
	         }else 
	         {
	             return 0;
	         }
	     }
	 
	    public DpaUploadManagedBean() {
	     }
	 
		@PostConstruct
		public void init() {
			InitialContext ic;
				
			try {
					ic = new InitialContext();
					facade = (MZRFacadeLocal) ic.lookup("MZRFacade/local");
				} catch (NamingException e) {
					logger.error(e.getMessage(),e);
				}
				
				
		   }
	    
		/**
		 * this method is a listener for the uploads operation
		 * 
		 * @param event
		 * @throws Exception
		 */
	     public void listener(UploadEvent event) throws Exception{
	         UploadItem item = event.getUploadItem();
	         DpaFile file = new DpaFile();
	         file.setLength(item.getFileSize());
	         file.setName(event.getUploadItem().getFileName());
	         file.setState(DpaFile.PENDEING);
	         file.setPath(item.getFile().toString());
	         files.add(file);
	         uploadsAvailable--;
	     }  
	       
	     public String clearUploadData() {
	         files.clear();
	         setUploadsAvailable(5);
	         return null;
	     }
	     
	     public long getTimeStamp(){
	         return System.currentTimeMillis();
	     }
	     
	     public ArrayList<DpaFile> getFiles() {
	         return files;
	     }
	 
	    public void setFiles(ArrayList<DpaFile> files) { 
	         this.files = files;
	     }
	 
	    public int getUploadsAvailable() {
	         return uploadsAvailable;
	     }
	 
	    public void setUploadsAvailable(int uploadsAvailable) {
	         this.uploadsAvailable = uploadsAvailable;
	     }
	 
	    public boolean isAutoUpload() {
	         return autoUpload;
	     }
	 
	    public void setAutoUpload(boolean autoUpload) {
	         this.autoUpload = autoUpload;
	     }
	 
	    public boolean isUseFlash() {
	         return useFlash;
	     }
	 
	    public void setUseFlash(boolean useFlash) {
	         this.useFlash = useFlash;
	     }
	    /**
	     * This method is called from the JSF when the user wants to load the files to 
	     * the system
	     * 
	     * 
	     */
	    public void pasreFiles(){
	    	
	    	try{
		    	for (DpaFile file : files) {
		    		if (!file.getState().equals(DpaFile.SUCCESS)){
		    			logger.debug("paring the execl file :"+file.getPath());
		    			facade.loadDPAAnswerSheet(file.getPath());
			    		file.setState(DpaFile.SUCCESS);	
		    		}
		    		
				}
	    	}catch (MZRInternalServerException e) {
	    		logger.error(e.getMessage(),e);
			} catch (MZRApplicativeException e) {
				logger.error(e.getMessage(),e);
			}
	    	
	    	
	    }


	

}
